package ui.panels;

import entities.Department;
import entities.User;
import java.awt.Color;
import java.util.Calendar;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import manager.GenericDriver;
import manager.ManagerDriver;
import model.ViewDepartmentComboBoxModel;
import ui.windows.RemedySchedulerFrame;

public class CreateEmployeePanel extends javax.swing.JPanel {

    /** Creates new form CreateEmployeePanel */
    public CreateEmployeePanel(RemedySchedulerFrame mainFrame, EmployeeManagementPanel parentPanel) {
        this.mainFrame = mainFrame;
        this.parentPanel = parentPanel;
        driver = (ManagerDriver) mainFrame.getDriver();
        genDriver = mainFrame.getGenericDriver();
        initComponents();
        includeModel();
    }

    private void includeModel() {
        roleModel = new DefaultComboBoxModel<String>(roles);
        roleModel.setSelectedItem("Employee");
        levelModel = new DefaultComboBoxModel<String>(levels[2]);
        cbRoles.setModel(roleModel);
        cbLevels.setModel(levelModel);

        depModel = new ViewDepartmentComboBoxModel(genDriver.showDepartment());
        depModel.setViewEmployee();
        cbDeps.setModel(depModel);

        checkID = false;
        checkDate = false;
        lbCheckDate.setText("");
        checkID();
    }

    private void createEmployee() {
        checkID();
        checkDate();
        if (checkID && checkDate) {
            Department dep = (Department) cbDeps.getSelectedItem();
            if (dep != null) {
                int id = dep.getDepartmentID();
                int role = cbRoles.getSelectedIndex() + 2;
                int level = cbLevels.getSelectedIndex() + 1;
                Date birthDate = dcBirthDate.getDate();
                User employee = new User();
                employee.setLoginID(txtID.getText());
                employee.setName(txtName.getText());
                employee.setDepartmentID(id);
                employee.setRole(role);
                employee.setLevel(level);
                employee.setMale(rbMale.isSelected());
                employee.setBirthDate(birthDate);
                employee.setPhone(txtPhone.getText());
                employee.setEmailAddress(txtEmail.getText());
                employee.setAddress(txaAddress.getText());
                boolean returnVal = parentPanel.createEmployee(employee);
                if (returnVal) {
                    clear();
                    close();
                    JOptionPane.showMessageDialog(null, "New employee is created", "message", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "Cannot create !", "Error", JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null, "You migh chose department !");
            }
        }
    }

    private void checkID() {
        String id = txtID.getText();
        if (id != null && id.length() != 0) {
            boolean returnVal = parentPanel.containtID(id);
            if (!returnVal) {
                lbCheckID.setText("valid");
                lbCheckID.setForeground(new Color(102, 102, 102));
                checkID = true;
            } else {
                lbCheckID.setText("existed !");
                lbCheckID.setForeground(Color.red);
                checkID = false;
            }
        } else {
            lbCheckID.setText("enter id...");
            lbCheckID.setForeground(Color.blue);
            checkID = false;
        }
    }

    private void checkDate() {
        // lay ra birthDate nhap vao
        Date birthDate = dcBirthDate.getDate();
        birthDate = new Date(birthDate.getYear() + 18, birthDate.getMonth(), birthDate.getDate());
        
        // neu da nhap vao ngay thang kiem tra ngay thang nhap vao phai thoa nhan vien phai tren 18 tuoi
        if (birthDate != null) {
            if (birthDate.before(Calendar.getInstance().getTime())) {
                lbCheckDate.setText("hop le");
                checkDate = true;
            } else {
                lbCheckDate.setText("tuoi phai tren 18");
                checkDate = false;
            }
        } else {
            lbCheckDate.setText("phai nhap vao ngay sinh");
            checkDate = false;
        }
    }

    private void setRole() {
        int choise = cbRoles.getSelectedIndex();
        if (choise != -1) {
            levelModel = new DefaultComboBoxModel<String>(levels[choise]);
            cbLevels.setModel(levelModel);
            cbLevels.setSelectedIndex(0);
        }
    }

    private void clear() {
        txtID.setText("");
        txtName.setText("");
        cbDeps.setSelectedItem(null);
        cbRoles.setSelectedIndex(2);
        levelModel = new DefaultComboBoxModel<String>(levels[2]);
        cbLevels.setSelectedIndex(0);
        rbMale.setSelected(true);
        dcBirthDate.setDate(null);
        txtPhone.setText("");
        txtEmail.setText("");
        txaAddress.setText("");
        lbCheckID.setText("");
        lbCheckDate.setText("");
    }

    private void close() {
        parentPanel.closeTarget();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        txtPhone = new javax.swing.JTextField();
        jLabel8 = new javax.swing.JLabel();
        txtEmail = new javax.swing.JTextField();
        jLabel9 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txaAddress = new javax.swing.JTextArea();
        txtName = new javax.swing.JTextField();
        cbDeps = new javax.swing.JComboBox();
        cbRoles = new javax.swing.JComboBox();
        cbLevels = new javax.swing.JComboBox();
        rbMale = new javax.swing.JRadioButton();
        rbFemale = new javax.swing.JRadioButton();
        jLabel12 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        lbCheckID = new javax.swing.JLabel();
        dcBirthDate = new com.toedter.calendar.JDateChooser();
        lbCheckDate = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        lbClose = new javax.swing.JLabel();

        setBackground(new java.awt.Color(255, 255, 255));

        jPanel1.setBackground(new java.awt.Color(255, 255, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Create New Employee", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Agency FB", 1, 14))); // NOI18N

        jLabel1.setText("Name:");

        jLabel2.setText("Department:");

        jLabel3.setText("Role:");

        jLabel4.setText("Level:");

        jLabel5.setText("Gender:");

        jLabel6.setText("BirthDate:");

        jLabel7.setText("Phone:");

        jLabel8.setText("Email:");

        jLabel9.setText("Address:");

        txaAddress.setColumns(20);
        txaAddress.setRows(5);
        jScrollPane1.setViewportView(txaAddress);

        cbDeps.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        cbRoles.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        cbRoles.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbRolesActionPerformed(evt);
            }
        });

        cbLevels.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        rbMale.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rbMale);
        rbMale.setSelected(true);
        rbMale.setText("male");

        rbFemale.setBackground(new java.awt.Color(255, 255, 255));
        buttonGroup1.add(rbFemale);
        rbFemale.setText("female");

        jLabel12.setText("ID:");

        txtID.addCaretListener(new javax.swing.event.CaretListener() {
            public void caretUpdate(javax.swing.event.CaretEvent evt) {
                txtIDCaretUpdate(evt);
            }
        });

        lbCheckID.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckID.setText("value");

        dcBirthDate.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                dcBirthDateMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                dcBirthDateMouseEntered(evt);
            }
        });

        lbCheckDate.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lbCheckDate.setText("value");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(59, 59, 59)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel4)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel12)
                            .addComponent(jLabel6))
                        .addGap(60, 60, 60)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(rbMale)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(rbFemale))
                            .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(cbLevels, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cbRoles, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cbDeps, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(txtName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 160, Short.MAX_VALUE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, 122, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbCheckID))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(dcBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, 139, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(lbCheckDate))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(37, 37, 37)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel9)
                            .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel8)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, 193, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jScrollPane1))))
                .addGap(34, 34, 34))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(txtID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbCheckID))
                .addGap(5, 5, 5)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cbDeps, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(cbRoles, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(cbLevels, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel5)
                            .addComponent(rbMale)
                            .addComponent(rbFemale))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel6)
                            .addComponent(dcBirthDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(lbCheckDate))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel7)
                    .addComponent(txtPhone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel8)
                    .addComponent(txtEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel9)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jButton1.setText("Create");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        lbClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/ui/images/close_20.png"))); // NOI18N
        lbClose.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                lbCloseMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 491, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 79, Short.MAX_VALUE)
                .addComponent(lbClose)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbClose)
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap(25, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

private void txtIDCaretUpdate(javax.swing.event.CaretEvent evt) {//GEN-FIRST:event_txtIDCaretUpdate
    checkID();
}//GEN-LAST:event_txtIDCaretUpdate

private void cbRolesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbRolesActionPerformed
    setRole();
}//GEN-LAST:event_cbRolesActionPerformed

private void lbCloseMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_lbCloseMouseClicked
    close();
}//GEN-LAST:event_lbCloseMouseClicked

private void dcBirthDateMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dcBirthDateMouseClicked
    checkDate();
}//GEN-LAST:event_dcBirthDateMouseClicked

private void dcBirthDateMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_dcBirthDateMouseEntered
    checkDate();
}//GEN-LAST:event_dcBirthDateMouseEntered

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    createEmployee();
}//GEN-LAST:event_jButton1ActionPerformed
    private RemedySchedulerFrame mainFrame;
    private EmployeeManagementPanel parentPanel;
    private ManagerDriver driver;
    private GenericDriver genDriver;
    private DefaultComboBoxModel<String> roleModel, levelModel;
    private ViewDepartmentComboBoxModel depModel;
    private String[] roles = {"Administractor", "Department Head", "Employee"};
    private String[][] levels = {{"Head", "Employee"},
        {"Head", "Immediate Head", "Next Immediate Head", "Junior Head "},
        {"Doctor", "Staff"}};
    private boolean checkID;
    private boolean checkDate;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JComboBox cbDeps;
    private javax.swing.JComboBox cbLevels;
    private javax.swing.JComboBox cbRoles;
    private com.toedter.calendar.JDateChooser dcBirthDate;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbCheckDate;
    private javax.swing.JLabel lbCheckID;
    private javax.swing.JLabel lbClose;
    private javax.swing.JRadioButton rbFemale;
    private javax.swing.JRadioButton rbMale;
    private javax.swing.JTextArea txaAddress;
    private javax.swing.JTextField txtEmail;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtName;
    private javax.swing.JTextField txtPhone;
    // End of variables declaration//GEN-END:variables
}
